/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.daos;

import com.insurance.cis.connection.CisConnection;
import com.insurance.cis.models.Group;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TruongLQ
 */
public class GroupDao {
    
    public Group getGroupById(int groupId) {
        try {
            final String GET_GROUP_BY_ID = "select * from [Group] where GroupID=?";
            PreparedStatement pst = CisConnection.getInstance()
                    .getSqlConnection().prepareStatement(GET_GROUP_BY_ID);
            pst.setInt(1, groupId);
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                return new Group(rs.getInt(1), rs.getString(2));
            }
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(GroupDao.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public Vector<Group> getAllGroups() {
        final String SQL = "select * from [Group]";
        try {
            Statement st = CisConnection.getInstance()
                    .getSqlConnection().createStatement();
            ResultSet rs = st.executeQuery(SQL);
            Vector<Group> groups = new Vector<>();
            while (rs.next()) {
                groups.add(new Group(rs.getInt(1), rs.getString(2)));
            }
            return groups;
        } catch (SQLException ex) {
            Logger.getLogger(GroupDao.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}
